package com.zelin.gulimall.product.service.impl;

import com.zelin.gulimall.product.service.AttrService;
import com.zelin.gulimall.product.vo.BaseAttrs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.gulimall.product.dao.ProductAttrValueDao;
import com.zelin.gulimall.product.entity.ProductAttrValueEntity;
import com.zelin.gulimall.product.service.ProductAttrValueService;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    //保存pms_product_attr_value
    @Override
    public void saveProductAttrValue(Long spuId,List<BaseAttrs> baseAttrs) {
        List<ProductAttrValueEntity> entities = baseAttrs.stream().map(m -> {
            ProductAttrValueEntity entity = new ProductAttrValueEntity();
            entity.setAttrId(m.getAttrId());
            String attrName = attrService.getById(m.getAttrId()).getAttrName();
            entity.setAttrName(attrName);
            entity.setAttrValue(m.getAttrValues());
            entity.setQuickShow(m.getShowDesc());
            entity.setSpuId(spuId);
            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(entities);
    }

    //在pms_product_attr_value根据spuId得到Attrids列表
    @Override
    public List<ProductAttrValueEntity> getAttrIdsBySpuId(Long spuId) {
        //1.根据spuId查询出ProductAttrValueEntity对象集合
        List<ProductAttrValueEntity> attrValueEntities = this.baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));


        return attrValueEntities;
    }

}